<template>
  <div class="electricity-container">
    <!-- Thermal Energy Storage -->
    <div class="storage-section">
      <h2 class="title">热储能</h2>
      <div class="info-grid">
        <div class="info-card">
          <label class="info-label">容量</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage1" class="form-input small">
            <span class="info-value">MWh</span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">放电效率</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage2" class="form-input small">
            <span class="info-value">%</span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">充电效率</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage3" class="form-input small">
            <span class="info-value">%</span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">充电功率</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage4" class="form-input small">
            <span class="info-value">MW</span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">放电功率</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage5" class="form-input small">
            <span class="info-value">MW</span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">初始SOC</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage6" class="form-input small">
            <span class="info-value">%</span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">最大SOC</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage7" class="form-input small">
            <span class="info-value">%</span>
          </div>
        </div>

        <div class="info-card">
          <label class="info-label">最小SOC</label>
          <div class="info-content">
            <input type="number" v-model="thermalStorage8" class="form-input small">
            <span class="info-value">%</span>
          </div>
        </div>
        <button class="distribution-btn emission-submit-btn" @click="submit">
          <span class="btn-text">提交</span>

        </button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, type Ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const submit = async () => {
  try {
    const payloads = [
      { id: 1, value: parseFloat(thermalStorage1.value) || 0 },
      { id: 2, value: parseFloat(thermalStorage2.value) || 0 },
      { id: 3, value: parseFloat(thermalStorage3.value) || 0 },
      { id: 4, value: parseFloat(thermalStorage4.value) || 0 },
      { id: 5, value: parseFloat(thermalStorage5.value) || 0 },
      { id: 6, value: parseFloat(thermalStorage6.value) || 0 },
      { id: 5, value: parseFloat(thermalStorage7.value) || 0 },
      { id: 6, value: parseFloat(thermalStorage8.value) || 0 }
    ];

    for (const item of payloads) {
      await axios.put('http://localhost:8080/diancn', item);
    }

    ElMessage.success('已成功更新到数据库');
  } catch (error) {
    console.error('提交失败:', error);
    ElMessage.error('提交失败，请检查数据格式或网络连接');
  }
};

const fetchAndSet = async (url: string, variable: Ref<string>) => {
  try {
    const response = await axios.get(url);
    variable.value = response.data.value;
  } catch  {
    ElMessage.error(`加载 ${url} 失败`);
  }
};

onMounted(async () => {

    await fetchAndSet('http://localhost:8080/recn/1', thermalStorage1);
    await fetchAndSet('http://localhost:8080/recn/2', thermalStorage2);
    await fetchAndSet('http://localhost:8080/recn/3', thermalStorage3);
    await fetchAndSet('http://localhost:8080/recn/4', thermalStorage4);
    await fetchAndSet('http://localhost:8080/recn/5', thermalStorage5);
    await fetchAndSet('http://localhost:8080/recn/6', thermalStorage6);
    await fetchAndSet('http://localhost:8080/recn/7', thermalStorage7);
    await fetchAndSet('http://localhost:8080/recn/8', thermalStorage8);  // 修改为你的真实接口

})
    const thermalStorage1 = ref('')
    const thermalStorage2 = ref('')
    const thermalStorage3 = ref('')
    const thermalStorage4 = ref('')
    const thermalStorage5 = ref('')
    const thermalStorage6 = ref('')
    const thermalStorage7 = ref('')
    const thermalStorage8 = ref('')




</script>

<style scoped>
.submit-emission-container {
  text-align: center;
  margin-top: 24px;
}

.emission-submit-btn {
  background-color: #7c3aed; /* 绿色 */
  padding: 12px 24px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 4px 10px rgba(22, 163, 74, 0.2);
}

.emission-submit-btn:hover {
  background-color: #7c3aed;
  box-shadow: 0 6px 16px rgba(22, 163, 74, 0.3);
}
.electricity-container {
  background-color: #f8fafc;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  border-radius: 14px;
  padding: 36px;
  max-width: 920px;
  margin: 0 auto;
  font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
  color: #1e293b;
}

.title {
  color: #4c1d95;
  text-align: center;
  margin-bottom: 32px;
  font-size: 28px;
  font-weight: 700;
  padding-bottom: 16px;
  border-bottom: 2px solid #7c3aed;
  letter-spacing: 0.5px;
}

.storage-section {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 28px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 18px;
}

.info-card {
  background-color: #f8fafc;
  padding: 18px;
  border-radius: 10px;
  border-left: 4px solid #7c3aed;
  transition: transform 0.2s, box-shadow 0.2s;
}

.info-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.info-label {
  display: block;
  font-weight: 500;
  color: #475569;
  margin-bottom: 10px;
  font-size: 15px;
}

.info-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.form-input {
  padding: 10px 16px;
  border: 1px solid #053676;
  border-radius: 6px;
  width: 120px;
  text-align: right;
  font-size: 15px;
  background: transparent;
  color: #1e293b;
  border: 1px solid #053676;
}

.form-input.small {
  width: 100px;
}

.form-input:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.2);
}

.info-value {
  font-weight: 600;
  color: #4c1d95;
  font-size: 15px;
  white-space: nowrap;
}

.save-btn {
  background-color: #7c3aed;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s;
  margin-top: 20px;
}

.save-btn:hover {
  background-color: #6d28d9;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(124, 58, 237, 0.3);
}

@media (max-width: 768px) {
  .electricity-container {
    padding: 24px;
    border-radius: 0;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  .form-input {
    width: 100%;
  }
}
</style>
